﻿using System;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;
using Mitosoft.Definitions.ViewModels;
using TextFileReaderModule.Notifications;

namespace TextFileReaderModule.ViewModels
{
  public class SaveCommentsDialogViewModel : BaseViewModel, IInteractionRequestAware
  {
    #region fields

    protected SaveCommentsDialogNotification _saveCommentsDialogNotification;

    #endregion

    #region constructor(s)

    /// <summary>
    /// Creates an <see cref="SaveCommentsDialogViewModel"/> instance.
    /// </summary>
    public SaveCommentsDialogViewModel()
    {
      DiscardDialogCommand = new DelegateCommand(DiscardDialogCommandInteraction);
      SaveDialogCommand = new DelegateCommand(SaveDialogCommandInteraction);
      CancelDialogCommand = new DelegateCommand(CancelDialogCommandInteraction);
    }

    #endregion

    #region properties

    /// <summary>
    /// Command used when the dialog was canceled.
    /// </summary>
    public ICommand DiscardDialogCommand { get; private set; }

    /// <summary>
    /// Command used when the dialog was accepted.
    /// </summary>
    public ICommand SaveDialogCommand { get; private set; }

    /// <summary>
    /// Command used when the dialog was negated.
    /// </summary>
    public ICommand CancelDialogCommand { get; private set; }

    #endregion

    #region methods

    /// <summary>
    /// <see cref="DiscardDialogCommand"/> was executed.
    /// </summary>
    protected void DiscardDialogCommandInteraction()
    {
      if (Notification != null)
      {
        _saveCommentsDialogNotification.CommentsShouldBeSaved = false;
        _saveCommentsDialogNotification.Confirmed = false;
      }

      FinishInteraction();
    }

    /// <summary>
    /// <see cref="SaveDialogCommand"/> was executed.
    /// </summary>
    protected void SaveDialogCommandInteraction()
    {
      if (Notification != null)
      {
        _saveCommentsDialogNotification.CommentsShouldBeSaved = true;
        _saveCommentsDialogNotification.Confirmed = true;
      }

      FinishInteraction();
    }

    /// <summary>
    /// <see cref="CancelDialogCommand"/> was executed.
    /// </summary>
    protected void CancelDialogCommandInteraction()
    {
      if (Notification != null)
      {
        _saveCommentsDialogNotification.CommentsShouldBeSaved = null;
        _saveCommentsDialogNotification.Confirmed = false;
      }

      FinishInteraction();
    }

    #endregion

    #region IInteractionRequestAware

    /// <summary>
    /// Gets or sets the Finish - action.
    /// </summary>
    public Action FinishInteraction
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the Notification - action.
    /// </summary>
    public INotification Notification
    {
      get { return _saveCommentsDialogNotification; }
      set
      {
        if (!(value is SaveCommentsDialogNotification)) return;

        // To keep the code simple, this is the only property where we are raising the PropertyChanged event,
        // as it's required to update the bindings when this property is populated.
        // Usually you would want to raise this event for other properties too.
        _saveCommentsDialogNotification = value as SaveCommentsDialogNotification;

        HandlePropertyChanged();
      }
    }

    #endregion
  }
}
